Case Study Outline

Install required packages

library(tidyverse)  # helps wrangle data
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)  # helps wrangle date attributes
library(ggplot2)  # helps visualize data
library(readxl)
getwd() # displays working directory
## [1] "/Users/bradleycardona/Documents/data_analytics/fertility_rate_case_study"
# set working directory to simplify calls to data
setwd("~/Documents/data_analytics/fertility_rate_case_study/") 

Step 1: Collect data

fertility_rates <- read_xls("fertility_rates.xls")
head(fertility_rates)
## # A tibble: 6 × 67
##   `Country Name`  `Country Code` `Indicator Name` `Indicator Code` `1960` `1961`
##   <chr>           <chr>          <chr>            <chr>             <dbl>  <dbl>
## 1 Aruba           ABW            Fertility rate,… SP.DYN.TFRT.IN     4.82   4.66
## 2 Africa Eastern… AFE            Fertility rate,… SP.DYN.TFRT.IN     6.72   6.74
## 3 Afghanistan     AFG            Fertility rate,… SP.DYN.TFRT.IN     7.28   7.28
## 4 Africa Western… AFW            Fertility rate,… SP.DYN.TFRT.IN     6.46   6.47
## 5 Angola          AGO            Fertility rate,… SP.DYN.TFRT.IN     6.71   6.79
## 6 Albania         ALB            Fertility rate,… SP.DYN.TFRT.IN     6.46   6.35
## # ℹ 61 more variables: `1962` <dbl>, `1963` <dbl>, `1964` <dbl>, `1965` <dbl>,
## #   `1966` <dbl>, `1967` <dbl>, `1968` <dbl>, `1969` <dbl>, `1970` <dbl>,
## #   `1971` <dbl>, `1972` <dbl>, `1973` <dbl>, `1974` <dbl>, `1975` <dbl>,
## #   `1976` <dbl>, `1977` <dbl>, `1978` <dbl>, `1979` <dbl>, `1980` <dbl>,
## #   `1981` <dbl>, `1982` <dbl>, `1983` <dbl>, `1984` <dbl>, `1985` <dbl>,
## #   `1986` <dbl>, `1987` <dbl>, `1988` <dbl>, `1989` <dbl>, `1990` <dbl>,
## #   `1991` <dbl>, `1992` <dbl>, `1993` <dbl>, `1994` <dbl>, `1995` <dbl>, …

Step 2: Wrangle data, look for any incongruencies

colnames(fertility_rates)  # Column names
##  [1] "Country Name"   "Country Code"   "Indicator Name" "Indicator Code"
##  [5] "1960"           "1961"           "1962"           "1963"          
##  [9] "1964"           "1965"           "1966"           "1967"          
## [13] "1968"           "1969"           "1970"           "1971"          
## [17] "1972"           "1973"           "1974"           "1975"          
## [21] "1976"           "1977"           "1978"           "1979"          
## [25] "1980"           "1981"           "1982"           "1983"          
## [29] "1984"           "1985"           "1986"           "1987"          
## [33] "1988"           "1989"           "1990"           "1991"          
## [37] "1992"           "1993"           "1994"           "1995"          
## [41] "1996"           "1997"           "1998"           "1999"          
## [45] "2000"           "2001"           "2002"           "2003"          
## [49] "2004"           "2005"           "2006"           "2007"          
## [53] "2008"           "2009"           "2010"           "2011"          
## [57] "2012"           "2013"           "2014"           "2015"          
## [61] "2016"           "2017"           "2018"           "2019"          
## [65] "2020"           "2021"           "2022"
nrow(fertility_rates)  # Number of rows
## [1] 266
dim(fertility_rates)  # Dimensionss
## [1] 266  67
head(fertility_rates)  # First 6 rows of data frame
## # A tibble: 6 × 67
##   `Country Name`  `Country Code` `Indicator Name` `Indicator Code` `1960` `1961`
##   <chr>           <chr>          <chr>            <chr>             <dbl>  <dbl>
## 1 Aruba           ABW            Fertility rate,… SP.DYN.TFRT.IN     4.82   4.66
## 2 Africa Eastern… AFE            Fertility rate,… SP.DYN.TFRT.IN     6.72   6.74
## 3 Afghanistan     AFG            Fertility rate,… SP.DYN.TFRT.IN     7.28   7.28
## 4 Africa Western… AFW            Fertility rate,… SP.DYN.TFRT.IN     6.46   6.47
## 5 Angola          AGO            Fertility rate,… SP.DYN.TFRT.IN     6.71   6.79
## 6 Albania         ALB            Fertility rate,… SP.DYN.TFRT.IN     6.46   6.35
## # ℹ 61 more variables: `1962` <dbl>, `1963` <dbl>, `1964` <dbl>, `1965` <dbl>,
## #   `1966` <dbl>, `1967` <dbl>, `1968` <dbl>, `1969` <dbl>, `1970` <dbl>,
## #   `1971` <dbl>, `1972` <dbl>, `1973` <dbl>, `1974` <dbl>, `1975` <dbl>,
## #   `1976` <dbl>, `1977` <dbl>, `1978` <dbl>, `1979` <dbl>, `1980` <dbl>,
## #   `1981` <dbl>, `1982` <dbl>, `1983` <dbl>, `1984` <dbl>, `1985` <dbl>,
## #   `1986` <dbl>, `1987` <dbl>, `1988` <dbl>, `1989` <dbl>, `1990` <dbl>,
## #   `1991` <dbl>, `1992` <dbl>, `1993` <dbl>, `1994` <dbl>, `1995` <dbl>, …
str(fertility_rates)  # Columns and respective data types (numeric, character, etc)
## tibble [266 × 67] (S3: tbl_df/tbl/data.frame)
##  $ Country Name  : chr [1:266] "Aruba" "Africa Eastern and Southern" "Afghanistan" "Africa Western and Central" ...
##  $ Country Code  : chr [1:266] "ABW" "AFE" "AFG" "AFW" ...
##  $ Indicator Name: chr [1:266] "Fertility rate, total (births per woman)" "Fertility rate, total (births per woman)" "Fertility rate, total (births per woman)" "Fertility rate, total (births per woman)" ...
##  $ Indicator Code: chr [1:266] "SP.DYN.TFRT.IN" "SP.DYN.TFRT.IN" "SP.DYN.TFRT.IN" "SP.DYN.TFRT.IN" ...
##  $ 1960          : num [1:266] 4.82 6.72 7.28 6.46 6.71 ...
##  $ 1961          : num [1:266] 4.66 6.74 7.28 6.47 6.79 ...
##  $ 1962          : num [1:266] 4.47 6.76 7.29 6.49 6.87 ...
##  $ 1963          : num [1:266] 4.27 6.78 7.3 6.51 6.95 ...
##  $ 1964          : num [1:266] 4.06 6.79 7.3 6.53 7.04 ...
##  $ 1965          : num [1:266] 3.84 6.8 7.3 6.54 7.12 ...
##  $ 1966          : num [1:266] 3.62 6.81 7.32 6.56 7.19 ...
##  $ 1967          : num [1:266] 3.42 6.82 7.34 6.59 7.27 ...
##  $ 1968          : num [1:266] 3.23 6.83 7.36 6.61 7.33 ...
##  $ 1969          : num [1:266] 3.05 6.83 7.39 6.64 7.39 ...
##  $ 1970          : num [1:266] 2.91 6.84 7.4 6.66 7.43 ...
##  $ 1971          : num [1:266] 2.79 6.84 7.43 6.7 7.47 ...
##  $ 1972          : num [1:266] 2.69 6.84 7.45 6.73 7.49 ...
##  $ 1973          : num [1:266] 2.61 6.83 7.49 6.76 7.5 ...
##  $ 1974          : num [1:266] 2.55 6.82 7.53 6.8 7.5 ...
##  $ 1975          : num [1:266] 2.51 6.81 7.54 6.84 7.49 ...
##  $ 1976          : num [1:266] 2.47 6.79 7.56 6.86 7.49 ...
##  $ 1977          : num [1:266] 2.45 6.77 7.59 6.9 7.47 ...
##  $ 1978          : num [1:266] 2.42 6.75 7.6 6.92 7.47 ...
##  $ 1979          : num [1:266] 2.41 6.73 7.61 6.91 7.46 ...
##  $ 1980          : num [1:266] 2.39 6.7 7.59 6.9 7.46 ...
##  $ 1981          : num [1:266] 2.38 6.67 7.57 6.88 7.46 ...
##  $ 1982          : num [1:266] 2.36 6.64 7.55 6.86 7.46 ...
##  $ 1983          : num [1:266] 2.35 6.6 7.54 6.83 7.46 ...
##  $ 1984          : num [1:266] 2.34 6.57 7.51 6.78 7.46 ...
##  $ 1985          : num [1:266] 2.33 6.51 7.52 6.73 7.45 ...
##  $ 1986          : num [1:266] 2.32 6.46 7.52 6.68 7.43 ...
##  $ 1987          : num [1:266] 2.31 6.42 7.53 6.64 7.41 ...
##  $ 1988          : num [1:266] 2.29 6.34 7.53 6.6 7.37 ...
##  $ 1989          : num [1:266] 2.27 6.26 7.53 6.57 7.33 ...
##  $ 1990          : num [1:266] 2.3 6.17 7.57 6.52 7.27 ...
##  $ 1991          : num [1:266] 2.31 6.1 7.61 6.47 7.21 ...
##  $ 1992          : num [1:266] 2.28 6.03 7.67 6.42 7.14 ...
##  $ 1993          : num [1:266] 2.23 5.96 7.72 6.36 7.07 ...
##  $ 1994          : num [1:266] 2.12 5.9 7.72 6.3 6.99 ...
##  $ 1995          : num [1:266] 2.19 5.84 7.71 6.24 6.92 ...
##  $ 1996          : num [1:266] 2.15 5.77 7.71 6.17 6.85 ...
##  $ 1997          : num [1:266] 2.14 5.7 7.67 6.1 6.79 ...
##  $ 1998          : num [1:266] 1.96 5.64 7.64 6.04 6.73 ...
##  $ 1999          : num [1:266] 1.87 5.59 7.6 6.03 6.68 ...
##  $ 2000          : num [1:266] 1.9 5.52 7.53 6.02 6.64 ...
##  $ 2001          : num [1:266] 1.83 5.48 7.45 6 6.6 ...
##  $ 2002          : num [1:266] 1.76 5.43 7.34 5.97 6.57 ...
##  $ 2003          : num [1:266] 1.75 5.38 7.22 5.93 6.53 ...
##  $ 2004          : num [1:266] 1.68 5.34 7.07 5.89 6.5 ...
##  $ 2005          : num [1:266] 1.78 5.31 6.91 5.86 6.46 ...
##  $ 2006          : num [1:266] 1.91 5.27 6.72 5.85 6.42 ...
##  $ 2007          : num [1:266] 1.93 5.22 6.53 5.82 6.37 ...
##  $ 2008          : num [1:266] 1.94 5.19 6.38 5.79 6.32 ...
##  $ 2009          : num [1:266] 1.92 5.12 6.24 5.75 6.26 ...
##  $ 2010          : num [1:266] 1.94 5.04 6.1 5.7 6.19 ...
##  $ 2011          : num [1:266] 1.96 4.96 5.96 5.65 6.12 ...
##  $ 2012          : num [1:266] 2.03 4.88 5.83 5.58 6.04 ...
##  $ 2013          : num [1:266] 2.12 4.81 5.7 5.51 5.95 ...
##  $ 2014          : num [1:266] 2.15 4.74 5.56 5.44 5.86 ...
##  $ 2015          : num [1:266] 1.97 4.68 5.41 5.39 5.77 ...
##  $ 2016          : num [1:266] 1.95 4.62 5.26 5.33 5.69 ...
##  $ 2017          : num [1:266] 1.84 4.57 5.13 5.26 5.6 ...
##  $ 2018          : num [1:266] 1.59 4.53 5 5.19 5.52 ...
##  $ 2019          : num [1:266] 1.49 4.48 4.87 5.12 5.44 ...
##  $ 2020          : num [1:266] 1.32 4.42 4.75 5.05 5.37 ...
##  $ 2021          : num [1:266] 1.18 4.35 4.64 4.98 5.3 ...
##  $ 2022          : logi [1:266] NA NA NA NA NA NA ...
summary(fertility_rates)  # Statistical summary of data. Mainly for numerics
##  Country Name       Country Code       Indicator Name     Indicator Code    
##  Length:266         Length:266         Length:266         Length:266        
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##                                                                             
##       1960            1961            1962            1963      
##  Min.   :1.940   Min.   :1.940   Min.   :1.790   Min.   :1.820  
##  1st Qu.:4.240   1st Qu.:4.082   1st Qu.:4.190   1st Qu.:4.160  
##  Median :6.078   Median :6.083   Median :6.085   Median :6.147  
##  Mean   :5.444   Mean   :5.423   Mean   :5.456   Mean   :5.488  
##  3rd Qu.:6.721   3rd Qu.:6.725   3rd Qu.:6.747   3rd Qu.:6.771  
##  Max.   :8.234   Max.   :8.266   Max.   :8.285   Max.   :8.309  
##  NA's   :13      NA's   :14      NA's   :13      NA's   :14     
##       1964            1965            1966            1967      
##  Min.   :1.790   Min.   :1.740   Min.   :1.580   Min.   :1.800  
##  1st Qu.:4.059   1st Qu.:3.842   1st Qu.:3.709   1st Qu.:3.637  
##  Median :6.019   Median :5.960   Median :5.907   Median :5.825  
##  Mean   :5.426   Mean   :5.381   Mean   :5.323   Mean   :5.269  
##  3rd Qu.:6.734   3rd Qu.:6.733   3rd Qu.:6.707   3rd Qu.:6.683  
##  Max.   :8.330   Max.   :8.344   Max.   :8.356   Max.   :8.340  
##  NA's   :13      NA's   :13      NA's   :13      NA's   :13     
##       1968            1969            1970            1971      
##  Min.   :1.830   Min.   :1.870   Min.   :1.823   Min.   :1.680  
##  1st Qu.:3.460   1st Qu.:3.284   1st Qu.:3.188   1st Qu.:3.144  
##  Median :5.788   Median :5.738   Median :5.636   Median :5.534  
##  Mean   :5.227   Mean   :5.173   Mean   :5.115   Mean   :5.057  
##  3rd Qu.:6.682   3rd Qu.:6.679   3rd Qu.:6.680   3rd Qu.:6.654  
##  Max.   :8.315   Max.   :8.264   Max.   :8.238   Max.   :8.264  
##  NA's   :13      NA's   :13      NA's   :12      NA's   :12     
##       1972            1973            1974            1975      
##  Min.   :1.580   Min.   :1.490   Min.   :1.510   Min.   :1.450  
##  1st Qu.:3.060   1st Qu.:2.933   1st Qu.:2.889   1st Qu.:2.760  
##  Median :5.314   Median :5.252   Median :5.086   Median :4.975  
##  Mean   :4.987   Mean   :4.903   Mean   :4.831   Mean   :4.742  
##  3rd Qu.:6.599   3rd Qu.:6.630   3rd Qu.:6.608   3rd Qu.:6.586  
##  Max.   :8.299   Max.   :8.304   Max.   :8.335   Max.   :8.401  
##  NA's   :12      NA's   :12      NA's   :12      NA's   :11     
##       1976            1977            1978            1979      
##  Min.   :1.450   Min.   :1.400   Min.   :1.380   Min.   :1.380  
##  1st Qu.:2.731   1st Qu.:2.659   1st Qu.:2.577   1st Qu.:2.554  
##  Median :4.810   Median :4.706   Median :4.534   Median :4.493  
##  Mean   :4.678   Mean   :4.610   Mean   :4.555   Mean   :4.522  
##  3rd Qu.:6.554   3rd Qu.:6.512   3rd Qu.:6.474   3rd Qu.:6.463  
##  Max.   :8.444   Max.   :8.504   Max.   :8.520   Max.   :8.671  
##  NA's   :10      NA's   :11      NA's   :11      NA's   :11     
##       1980            1981            1982            1983      
##  Min.   :1.440   Min.   :1.430   Min.   :1.410   Min.   :1.330  
##  1st Qu.:2.471   1st Qu.:2.423   1st Qu.:2.384   1st Qu.:2.373  
##  Median :4.426   Median :4.314   Median :4.216   Median :4.095  
##  Mean   :4.479   Mean   :4.425   Mean   :4.381   Mean   :4.326  
##  3rd Qu.:6.399   3rd Qu.:6.375   3rd Qu.:6.371   3rd Qu.:6.303  
##  Max.   :8.710   Max.   :8.752   Max.   :8.793   Max.   :8.828  
##  NA's   :11      NA's   :11      NA's   :10      NA's   :11     
##       1984            1985            1986            1987      
##  Min.   :1.290   Min.   :1.370   Min.   :1.350   Min.   :1.311  
##  1st Qu.:2.340   1st Qu.:2.324   1st Qu.:2.321   1st Qu.:2.288  
##  Median :4.014   Median :3.959   Median :3.962   Median :3.788  
##  Mean   :4.270   Mean   :4.216   Mean   :4.164   Mean   :4.103  
##  3rd Qu.:6.229   3rd Qu.:6.138   3rd Qu.:6.018   3rd Qu.:5.856  
##  Max.   :8.853   Max.   :8.864   Max.   :8.858   Max.   :8.833  
##  NA's   :11      NA's   :11      NA's   :11      NA's   :10     
##       1988            1989            1990            1991      
##  Min.   :1.360   Min.   :1.296   Min.   :1.272   Min.   :1.281  
##  1st Qu.:2.280   1st Qu.:2.244   1st Qu.:2.304   1st Qu.:2.187  
##  Median :3.711   Median :3.583   Median :3.471   Median :3.402  
##  Mean   :4.050   Mean   :3.983   Mean   :3.931   Mean   :3.853  
##  3rd Qu.:5.798   3rd Qu.:5.692   3rd Qu.:5.604   3rd Qu.:5.474  
##  Max.   :8.786   Max.   :8.713   Max.   :8.606   Max.   :8.459  
##  NA's   :11      NA's   :11      NA's   :9       NA's   :10     
##       1992            1993            1994            1995      
##  Min.   :1.290   Min.   :1.250   Min.   :1.190   Min.   :1.160  
##  1st Qu.:2.120   1st Qu.:2.034   1st Qu.:1.987   1st Qu.:1.967  
##  Median :3.318   Median :3.208   Median :3.121   Median :3.072  
##  Mean   :3.776   Mean   :3.699   Mean   :3.623   Mean   :3.543  
##  3rd Qu.:5.314   3rd Qu.:5.203   3rd Qu.:5.046   3rd Qu.:4.884  
##  Max.   :8.272   Max.   :8.048   Max.   :7.989   Max.   :7.962  
##  NA's   :9       NA's   :10      NA's   :10      NA's   :9      
##       1996            1997            1998            1999      
##  Min.   :1.140   Min.   :1.090   Min.   :1.016   Min.   :0.981  
##  1st Qu.:1.931   1st Qu.:1.896   1st Qu.:1.860   1st Qu.:1.817  
##  Median :3.014   Median :2.940   Median :2.815   Median :2.767  
##  Mean   :3.476   Mean   :3.408   Mean   :3.339   Mean   :3.288  
##  3rd Qu.:4.771   3rd Qu.:4.660   3rd Qu.:4.576   3rd Qu.:4.516  
##  Max.   :7.985   Max.   :7.965   Max.   :7.817   Max.   :7.752  
##  NA's   :10      NA's   :9       NA's   :9       NA's   :9      
##       2000            2001            2002            2003      
##  Min.   :0.912   Min.   :0.840   Min.   :0.800   Min.   :0.792  
##  1st Qu.:1.855   1st Qu.:1.800   1st Qu.:1.790   1st Qu.:1.786  
##  Median :2.716   Median :2.667   Median :2.623   Median :2.583  
##  Mean   :3.235   Mean   :3.182   Mean   :3.136   Mean   :3.095  
##  3rd Qu.:4.434   3rd Qu.:4.375   3rd Qu.:4.304   3rd Qu.:4.234  
##  Max.   :7.732   Max.   :7.695   Max.   :7.671   Max.   :7.654  
##  NA's   :7       NA's   :8       NA's   :8       NA's   :8      
##       2004            2005            2006            2007      
##  Min.   :0.800   Min.   :0.834   Min.   :0.874   Min.   :0.918  
##  1st Qu.:1.781   1st Qu.:1.786   1st Qu.:1.793   1st Qu.:1.823  
##  Median :2.582   Median :2.552   Median :2.503   Median :2.510  
##  Mean   :3.068   Mean   :3.033   Mean   :3.007   Mean   :2.991  
##  3rd Qu.:4.129   3rd Qu.:3.983   3rd Qu.:3.850   3rd Qu.:3.863  
##  Max.   :7.634   Max.   :7.615   Max.   :7.579   Max.   :7.559  
##  NA's   :8       NA's   :7       NA's   :7       NA's   :7      
##       2008            2009            2010            2011      
##  Min.   :0.947   Min.   :0.986   Min.   :1.042   Min.   :1.115  
##  1st Qu.:1.838   1st Qu.:1.823   1st Qu.:1.802   1st Qu.:1.781  
##  Median :2.481   Median :2.437   Median :2.397   Median :2.334  
##  Mean   :2.983   Mean   :2.957   Mean   :2.924   Mean   :2.895  
##  3rd Qu.:3.877   3rd Qu.:3.865   3rd Qu.:3.880   3rd Qu.:3.836  
##  Max.   :7.539   Max.   :7.513   Max.   :7.485   Max.   :7.449  
##  NA's   :7       NA's   :7       NA's   :7       NA's   :8      
##       2012            2013            2014            2015      
##  Min.   :1.103   Min.   :1.080   Min.   :1.205   Min.   :1.186  
##  1st Qu.:1.792   1st Qu.:1.750   1st Qu.:1.751   1st Qu.:1.734  
##  Median :2.312   Median :2.328   Median :2.300   Median :2.260  
##  Mean   :2.868   Mean   :2.835   Mean   :2.813   Mean   :2.775  
##  3rd Qu.:3.753   3rd Qu.:3.688   3rd Qu.:3.632   3rd Qu.:3.560  
##  Max.   :7.400   Max.   :7.344   Max.   :7.279   Max.   :7.211  
##  NA's   :6       NA's   :8       NA's   :8       NA's   :7      
##       2016            2017            2018            2019      
##  Min.   :0.987   Min.   :0.872   Min.   :0.917   Min.   :0.918  
##  1st Qu.:1.725   1st Qu.:1.693   1st Qu.:1.648   1st Qu.:1.612  
##  Median :2.245   Median :2.211   Median :2.175   Median :2.139  
##  Mean   :2.742   Mean   :2.694   Mean   :2.653   Mean   :2.611  
##  3rd Qu.:3.492   3rd Qu.:3.432   3rd Qu.:3.403   3rd Qu.:3.333  
##  Max.   :7.141   Max.   :7.084   Max.   :7.023   Max.   :6.961  
##  NA's   :8       NA's   :8       NA's   :8       NA's   :8      
##       2020            2021         2022        
##  Min.   :0.837   Min.   :0.772   Mode:logical  
##  1st Qu.:1.572   1st Qu.:1.583   NA's:266      
##  Median :2.103   Median :2.088                 
##  Mean   :2.560   Mean   :2.542                 
##  3rd Qu.:3.271   3rd Qu.:3.288                 
##  Max.   :6.892   Max.   :6.820                 
##  NA's   :7       NA's   :8
# delete two columns: "Indicator Name" and "Indicator Code"
fertility_rates <- fertility_rates %>% 
  select(-c("Indicator Name", "Indicator Code"))

Step 3: Clean up data and prepare for analysis

Inspect the new table that has been created

colnames(fertility_rates)  # Column names
##  [1] "Country Name" "Country Code" "1960"         "1961"         "1962"        
##  [6] "1963"         "1964"         "1965"         "1966"         "1967"        
## [11] "1968"         "1969"         "1970"         "1971"         "1972"        
## [16] "1973"         "1974"         "1975"         "1976"         "1977"        
## [21] "1978"         "1979"         "1980"         "1981"         "1982"        
## [26] "1983"         "1984"         "1985"         "1986"         "1987"        
## [31] "1988"         "1989"         "1990"         "1991"         "1992"        
## [36] "1993"         "1994"         "1995"         "1996"         "1997"        
## [41] "1998"         "1999"         "2000"         "2001"         "2002"        
## [46] "2003"         "2004"         "2005"         "2006"         "2007"        
## [51] "2008"         "2009"         "2010"         "2011"         "2012"        
## [56] "2013"         "2014"         "2015"         "2016"         "2017"        
## [61] "2018"         "2019"         "2020"         "2021"         "2022"
nrow(fertility_rates)  # Number of rows
## [1] 266
dim(fertility_rates)  # Dimensions
## [1] 266  65
head(fertility_rates)  # First 6 rows of data frame
## # A tibble: 6 × 65
##   `Country Name` `Country Code` `1960` `1961` `1962` `1963` `1964` `1965` `1966`
##   <chr>          <chr>           <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
## 1 Aruba          ABW              4.82   4.66   4.47   4.27   4.06   3.84   3.62
## 2 Africa Easter… AFE              6.72   6.74   6.76   6.78   6.79   6.80   6.81
## 3 Afghanistan    AFG              7.28   7.28   7.29   7.30   7.30   7.30   7.32
## 4 Africa Wester… AFW              6.46   6.47   6.49   6.51   6.53   6.54   6.56
## 5 Angola         AGO              6.71   6.79   6.87   6.95   7.04   7.12   7.19
## 6 Albania        ALB              6.46   6.35   6.21   6.05   5.85   5.62   5.46
## # ℹ 56 more variables: `1967` <dbl>, `1968` <dbl>, `1969` <dbl>, `1970` <dbl>,
## #   `1971` <dbl>, `1972` <dbl>, `1973` <dbl>, `1974` <dbl>, `1975` <dbl>,
## #   `1976` <dbl>, `1977` <dbl>, `1978` <dbl>, `1979` <dbl>, `1980` <dbl>,
## #   `1981` <dbl>, `1982` <dbl>, `1983` <dbl>, `1984` <dbl>, `1985` <dbl>,
## #   `1986` <dbl>, `1987` <dbl>, `1988` <dbl>, `1989` <dbl>, `1990` <dbl>,
## #   `1991` <dbl>, `1992` <dbl>, `1993` <dbl>, `1994` <dbl>, `1995` <dbl>,
## #   `1996` <dbl>, `1997` <dbl>, `1998` <dbl>, `1999` <dbl>, `2000` <dbl>, …
str(fertility_rates)  # Columns and respective data types (numeric, character, etc)
## tibble [266 × 65] (S3: tbl_df/tbl/data.frame)
##  $ Country Name: chr [1:266] "Aruba" "Africa Eastern and Southern" "Afghanistan" "Africa Western and Central" ...
##  $ Country Code: chr [1:266] "ABW" "AFE" "AFG" "AFW" ...
##  $ 1960        : num [1:266] 4.82 6.72 7.28 6.46 6.71 ...
##  $ 1961        : num [1:266] 4.66 6.74 7.28 6.47 6.79 ...
##  $ 1962        : num [1:266] 4.47 6.76 7.29 6.49 6.87 ...
##  $ 1963        : num [1:266] 4.27 6.78 7.3 6.51 6.95 ...
##  $ 1964        : num [1:266] 4.06 6.79 7.3 6.53 7.04 ...
##  $ 1965        : num [1:266] 3.84 6.8 7.3 6.54 7.12 ...
##  $ 1966        : num [1:266] 3.62 6.81 7.32 6.56 7.19 ...
##  $ 1967        : num [1:266] 3.42 6.82 7.34 6.59 7.27 ...
##  $ 1968        : num [1:266] 3.23 6.83 7.36 6.61 7.33 ...
##  $ 1969        : num [1:266] 3.05 6.83 7.39 6.64 7.39 ...
##  $ 1970        : num [1:266] 2.91 6.84 7.4 6.66 7.43 ...
##  $ 1971        : num [1:266] 2.79 6.84 7.43 6.7 7.47 ...
##  $ 1972        : num [1:266] 2.69 6.84 7.45 6.73 7.49 ...
##  $ 1973        : num [1:266] 2.61 6.83 7.49 6.76 7.5 ...
##  $ 1974        : num [1:266] 2.55 6.82 7.53 6.8 7.5 ...
##  $ 1975        : num [1:266] 2.51 6.81 7.54 6.84 7.49 ...
##  $ 1976        : num [1:266] 2.47 6.79 7.56 6.86 7.49 ...
##  $ 1977        : num [1:266] 2.45 6.77 7.59 6.9 7.47 ...
##  $ 1978        : num [1:266] 2.42 6.75 7.6 6.92 7.47 ...
##  $ 1979        : num [1:266] 2.41 6.73 7.61 6.91 7.46 ...
##  $ 1980        : num [1:266] 2.39 6.7 7.59 6.9 7.46 ...
##  $ 1981        : num [1:266] 2.38 6.67 7.57 6.88 7.46 ...
##  $ 1982        : num [1:266] 2.36 6.64 7.55 6.86 7.46 ...
##  $ 1983        : num [1:266] 2.35 6.6 7.54 6.83 7.46 ...
##  $ 1984        : num [1:266] 2.34 6.57 7.51 6.78 7.46 ...
##  $ 1985        : num [1:266] 2.33 6.51 7.52 6.73 7.45 ...
##  $ 1986        : num [1:266] 2.32 6.46 7.52 6.68 7.43 ...
##  $ 1987        : num [1:266] 2.31 6.42 7.53 6.64 7.41 ...
##  $ 1988        : num [1:266] 2.29 6.34 7.53 6.6 7.37 ...
##  $ 1989        : num [1:266] 2.27 6.26 7.53 6.57 7.33 ...
##  $ 1990        : num [1:266] 2.3 6.17 7.57 6.52 7.27 ...
##  $ 1991        : num [1:266] 2.31 6.1 7.61 6.47 7.21 ...
##  $ 1992        : num [1:266] 2.28 6.03 7.67 6.42 7.14 ...
##  $ 1993        : num [1:266] 2.23 5.96 7.72 6.36 7.07 ...
##  $ 1994        : num [1:266] 2.12 5.9 7.72 6.3 6.99 ...
##  $ 1995        : num [1:266] 2.19 5.84 7.71 6.24 6.92 ...
##  $ 1996        : num [1:266] 2.15 5.77 7.71 6.17 6.85 ...
##  $ 1997        : num [1:266] 2.14 5.7 7.67 6.1 6.79 ...
##  $ 1998        : num [1:266] 1.96 5.64 7.64 6.04 6.73 ...
##  $ 1999        : num [1:266] 1.87 5.59 7.6 6.03 6.68 ...
##  $ 2000        : num [1:266] 1.9 5.52 7.53 6.02 6.64 ...
##  $ 2001        : num [1:266] 1.83 5.48 7.45 6 6.6 ...
##  $ 2002        : num [1:266] 1.76 5.43 7.34 5.97 6.57 ...
##  $ 2003        : num [1:266] 1.75 5.38 7.22 5.93 6.53 ...
##  $ 2004        : num [1:266] 1.68 5.34 7.07 5.89 6.5 ...
##  $ 2005        : num [1:266] 1.78 5.31 6.91 5.86 6.46 ...
##  $ 2006        : num [1:266] 1.91 5.27 6.72 5.85 6.42 ...
##  $ 2007        : num [1:266] 1.93 5.22 6.53 5.82 6.37 ...
##  $ 2008        : num [1:266] 1.94 5.19 6.38 5.79 6.32 ...
##  $ 2009        : num [1:266] 1.92 5.12 6.24 5.75 6.26 ...
##  $ 2010        : num [1:266] 1.94 5.04 6.1 5.7 6.19 ...
##  $ 2011        : num [1:266] 1.96 4.96 5.96 5.65 6.12 ...
##  $ 2012        : num [1:266] 2.03 4.88 5.83 5.58 6.04 ...
##  $ 2013        : num [1:266] 2.12 4.81 5.7 5.51 5.95 ...
##  $ 2014        : num [1:266] 2.15 4.74 5.56 5.44 5.86 ...
##  $ 2015        : num [1:266] 1.97 4.68 5.41 5.39 5.77 ...
##  $ 2016        : num [1:266] 1.95 4.62 5.26 5.33 5.69 ...
##  $ 2017        : num [1:266] 1.84 4.57 5.13 5.26 5.6 ...
##  $ 2018        : num [1:266] 1.59 4.53 5 5.19 5.52 ...
##  $ 2019        : num [1:266] 1.49 4.48 4.87 5.12 5.44 ...
##  $ 2020        : num [1:266] 1.32 4.42 4.75 5.05 5.37 ...
##  $ 2021        : num [1:266] 1.18 4.35 4.64 4.98 5.3 ...
##  $ 2022        : logi [1:266] NA NA NA NA NA NA ...
summary(fertility_rates)  # Statistical summary of data. Mainly for numerics
##  Country Name       Country Code            1960            1961      
##  Length:266         Length:266         Min.   :1.940   Min.   :1.940  
##  Class :character   Class :character   1st Qu.:4.240   1st Qu.:4.082  
##  Mode  :character   Mode  :character   Median :6.078   Median :6.083  
##                                        Mean   :5.444   Mean   :5.423  
##                                        3rd Qu.:6.721   3rd Qu.:6.725  
##                                        Max.   :8.234   Max.   :8.266  
##                                        NA's   :13      NA's   :14     
##       1962            1963            1964            1965      
##  Min.   :1.790   Min.   :1.820   Min.   :1.790   Min.   :1.740  
##  1st Qu.:4.190   1st Qu.:4.160   1st Qu.:4.059   1st Qu.:3.842  
##  Median :6.085   Median :6.147   Median :6.019   Median :5.960  
##  Mean   :5.456   Mean   :5.488   Mean   :5.426   Mean   :5.381  
##  3rd Qu.:6.747   3rd Qu.:6.771   3rd Qu.:6.734   3rd Qu.:6.733  
##  Max.   :8.285   Max.   :8.309   Max.   :8.330   Max.   :8.344  
##  NA's   :13      NA's   :14      NA's   :13      NA's   :13     
##       1966            1967            1968            1969      
##  Min.   :1.580   Min.   :1.800   Min.   :1.830   Min.   :1.870  
##  1st Qu.:3.709   1st Qu.:3.637   1st Qu.:3.460   1st Qu.:3.284  
##  Median :5.907   Median :5.825   Median :5.788   Median :5.738  
##  Mean   :5.323   Mean   :5.269   Mean   :5.227   Mean   :5.173  
##  3rd Qu.:6.707   3rd Qu.:6.683   3rd Qu.:6.682   3rd Qu.:6.679  
##  Max.   :8.356   Max.   :8.340   Max.   :8.315   Max.   :8.264  
##  NA's   :13      NA's   :13      NA's   :13      NA's   :13     
##       1970            1971            1972            1973      
##  Min.   :1.823   Min.   :1.680   Min.   :1.580   Min.   :1.490  
##  1st Qu.:3.188   1st Qu.:3.144   1st Qu.:3.060   1st Qu.:2.933  
##  Median :5.636   Median :5.534   Median :5.314   Median :5.252  
##  Mean   :5.115   Mean   :5.057   Mean   :4.987   Mean   :4.903  
##  3rd Qu.:6.680   3rd Qu.:6.654   3rd Qu.:6.599   3rd Qu.:6.630  
##  Max.   :8.238   Max.   :8.264   Max.   :8.299   Max.   :8.304  
##  NA's   :12      NA's   :12      NA's   :12      NA's   :12     
##       1974            1975            1976            1977      
##  Min.   :1.510   Min.   :1.450   Min.   :1.450   Min.   :1.400  
##  1st Qu.:2.889   1st Qu.:2.760   1st Qu.:2.731   1st Qu.:2.659  
##  Median :5.086   Median :4.975   Median :4.810   Median :4.706  
##  Mean   :4.831   Mean   :4.742   Mean   :4.678   Mean   :4.610  
##  3rd Qu.:6.608   3rd Qu.:6.586   3rd Qu.:6.554   3rd Qu.:6.512  
##  Max.   :8.335   Max.   :8.401   Max.   :8.444   Max.   :8.504  
##  NA's   :12      NA's   :11      NA's   :10      NA's   :11     
##       1978            1979            1980            1981      
##  Min.   :1.380   Min.   :1.380   Min.   :1.440   Min.   :1.430  
##  1st Qu.:2.577   1st Qu.:2.554   1st Qu.:2.471   1st Qu.:2.423  
##  Median :4.534   Median :4.493   Median :4.426   Median :4.314  
##  Mean   :4.555   Mean   :4.522   Mean   :4.479   Mean   :4.425  
##  3rd Qu.:6.474   3rd Qu.:6.463   3rd Qu.:6.399   3rd Qu.:6.375  
##  Max.   :8.520   Max.   :8.671   Max.   :8.710   Max.   :8.752  
##  NA's   :11      NA's   :11      NA's   :11      NA's   :11     
##       1982            1983            1984            1985      
##  Min.   :1.410   Min.   :1.330   Min.   :1.290   Min.   :1.370  
##  1st Qu.:2.384   1st Qu.:2.373   1st Qu.:2.340   1st Qu.:2.324  
##  Median :4.216   Median :4.095   Median :4.014   Median :3.959  
##  Mean   :4.381   Mean   :4.326   Mean   :4.270   Mean   :4.216  
##  3rd Qu.:6.371   3rd Qu.:6.303   3rd Qu.:6.229   3rd Qu.:6.138  
##  Max.   :8.793   Max.   :8.828   Max.   :8.853   Max.   :8.864  
##  NA's   :10      NA's   :11      NA's   :11      NA's   :11     
##       1986            1987            1988            1989      
##  Min.   :1.350   Min.   :1.311   Min.   :1.360   Min.   :1.296  
##  1st Qu.:2.321   1st Qu.:2.288   1st Qu.:2.280   1st Qu.:2.244  
##  Median :3.962   Median :3.788   Median :3.711   Median :3.583  
##  Mean   :4.164   Mean   :4.103   Mean   :4.050   Mean   :3.983  
##  3rd Qu.:6.018   3rd Qu.:5.856   3rd Qu.:5.798   3rd Qu.:5.692  
##  Max.   :8.858   Max.   :8.833   Max.   :8.786   Max.   :8.713  
##  NA's   :11      NA's   :10      NA's   :11      NA's   :11     
##       1990            1991            1992            1993      
##  Min.   :1.272   Min.   :1.281   Min.   :1.290   Min.   :1.250  
##  1st Qu.:2.304   1st Qu.:2.187   1st Qu.:2.120   1st Qu.:2.034  
##  Median :3.471   Median :3.402   Median :3.318   Median :3.208  
##  Mean   :3.931   Mean   :3.853   Mean   :3.776   Mean   :3.699  
##  3rd Qu.:5.604   3rd Qu.:5.474   3rd Qu.:5.314   3rd Qu.:5.203  
##  Max.   :8.606   Max.   :8.459   Max.   :8.272   Max.   :8.048  
##  NA's   :9       NA's   :10      NA's   :9       NA's   :10     
##       1994            1995            1996            1997      
##  Min.   :1.190   Min.   :1.160   Min.   :1.140   Min.   :1.090  
##  1st Qu.:1.987   1st Qu.:1.967   1st Qu.:1.931   1st Qu.:1.896  
##  Median :3.121   Median :3.072   Median :3.014   Median :2.940  
##  Mean   :3.623   Mean   :3.543   Mean   :3.476   Mean   :3.408  
##  3rd Qu.:5.046   3rd Qu.:4.884   3rd Qu.:4.771   3rd Qu.:4.660  
##  Max.   :7.989   Max.   :7.962   Max.   :7.985   Max.   :7.965  
##  NA's   :10      NA's   :9       NA's   :10      NA's   :9      
##       1998            1999            2000            2001      
##  Min.   :1.016   Min.   :0.981   Min.   :0.912   Min.   :0.840  
##  1st Qu.:1.860   1st Qu.:1.817   1st Qu.:1.855   1st Qu.:1.800  
##  Median :2.815   Median :2.767   Median :2.716   Median :2.667  
##  Mean   :3.339   Mean   :3.288   Mean   :3.235   Mean   :3.182  
##  3rd Qu.:4.576   3rd Qu.:4.516   3rd Qu.:4.434   3rd Qu.:4.375  
##  Max.   :7.817   Max.   :7.752   Max.   :7.732   Max.   :7.695  
##  NA's   :9       NA's   :9       NA's   :7       NA's   :8      
##       2002            2003            2004            2005      
##  Min.   :0.800   Min.   :0.792   Min.   :0.800   Min.   :0.834  
##  1st Qu.:1.790   1st Qu.:1.786   1st Qu.:1.781   1st Qu.:1.786  
##  Median :2.623   Median :2.583   Median :2.582   Median :2.552  
##  Mean   :3.136   Mean   :3.095   Mean   :3.068   Mean   :3.033  
##  3rd Qu.:4.304   3rd Qu.:4.234   3rd Qu.:4.129   3rd Qu.:3.983  
##  Max.   :7.671   Max.   :7.654   Max.   :7.634   Max.   :7.615  
##  NA's   :8       NA's   :8       NA's   :8       NA's   :7      
##       2006            2007            2008            2009      
##  Min.   :0.874   Min.   :0.918   Min.   :0.947   Min.   :0.986  
##  1st Qu.:1.793   1st Qu.:1.823   1st Qu.:1.838   1st Qu.:1.823  
##  Median :2.503   Median :2.510   Median :2.481   Median :2.437  
##  Mean   :3.007   Mean   :2.991   Mean   :2.983   Mean   :2.957  
##  3rd Qu.:3.850   3rd Qu.:3.863   3rd Qu.:3.877   3rd Qu.:3.865  
##  Max.   :7.579   Max.   :7.559   Max.   :7.539   Max.   :7.513  
##  NA's   :7       NA's   :7       NA's   :7       NA's   :7      
##       2010            2011            2012            2013      
##  Min.   :1.042   Min.   :1.115   Min.   :1.103   Min.   :1.080  
##  1st Qu.:1.802   1st Qu.:1.781   1st Qu.:1.792   1st Qu.:1.750  
##  Median :2.397   Median :2.334   Median :2.312   Median :2.328  
##  Mean   :2.924   Mean   :2.895   Mean   :2.868   Mean   :2.835  
##  3rd Qu.:3.880   3rd Qu.:3.836   3rd Qu.:3.753   3rd Qu.:3.688  
##  Max.   :7.485   Max.   :7.449   Max.   :7.400   Max.   :7.344  
##  NA's   :7       NA's   :8       NA's   :6       NA's   :8      
##       2014            2015            2016            2017      
##  Min.   :1.205   Min.   :1.186   Min.   :0.987   Min.   :0.872  
##  1st Qu.:1.751   1st Qu.:1.734   1st Qu.:1.725   1st Qu.:1.693  
##  Median :2.300   Median :2.260   Median :2.245   Median :2.211  
##  Mean   :2.813   Mean   :2.775   Mean   :2.742   Mean   :2.694  
##  3rd Qu.:3.632   3rd Qu.:3.560   3rd Qu.:3.492   3rd Qu.:3.432  
##  Max.   :7.279   Max.   :7.211   Max.   :7.141   Max.   :7.084  
##  NA's   :8       NA's   :7       NA's   :8       NA's   :8      
##       2018            2019            2020            2021         2022        
##  Min.   :0.917   Min.   :0.918   Min.   :0.837   Min.   :0.772   Mode:logical  
##  1st Qu.:1.648   1st Qu.:1.612   1st Qu.:1.572   1st Qu.:1.583   NA's:266      
##  Median :2.175   Median :2.139   Median :2.103   Median :2.088                 
##  Mean   :2.653   Mean   :2.611   Mean   :2.560   Mean   :2.542                 
##  3rd Qu.:3.403   3rd Qu.:3.333   3rd Qu.:3.271   3rd Qu.:3.288                 
##  Max.   :7.023   Max.   :6.961   Max.   :6.892   Max.   :6.820                 
##  NA's   :8       NA's   :8       NA's   :7       NA's   :8

Step 4: Conduct descriptive analysis

summary(fertility_rates$"1960")
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   1.940   4.240   6.078   5.444   6.721   8.234      13
# Compare Average Total Fertility rates in 1960 vs. Average Total Fertility rates in 2021
aggregate(fertility_rates$"1960" ~ fertility_rates$"2021", FUN = mean)
##     fertility_rates$"2021" fertility_rates$"1960"
## 1                 0.772000               5.067000
## 2                 0.808000               5.949000
## 3                 0.907000               4.796000
## 4                 1.005000               5.162000
## 5                 1.088000               4.933000
## 6                 1.120000               5.760000
## 7                 1.140000               3.620000
## 8                 1.160000               2.240000
## 9                 1.164000               4.451000
## 10                1.180000               4.820000
## 11                1.190000               2.860000
## 12                1.250000               2.400000
## 13                1.300000               2.755000
## 14                1.321000               3.512000
## 15                1.330000               2.980000
## 16                1.331000               6.248000
## 17                1.340000               2.560000
## 18                1.350000               3.907000
## 19                1.352000               5.580000
## 20                1.380000               2.725000
## 21                1.383908               4.801000
## 22                1.389000               4.818000
## 23                1.390000               4.342500
## 24                1.396865               4.378614
## 25                1.399000               6.967000
## 26                1.410000               6.167000
## 27                1.413000               6.704000
## 28                1.430000               3.811000
## 29                1.442000               4.129000
## 30                1.457000               2.270000
## 31                1.460000               4.719000
## 32                1.480000               2.401500
## 33                1.483000               2.670000
## 34                1.488552               2.897990
## 35                1.489433               4.561606
## 36                1.493000               2.673500
## 37                1.498219               4.531774
## 38                1.504510               2.605864
## 39                1.516846               4.864229
## 40                1.520000               4.159000
## 41                1.520288               2.571186
## 42                1.520442               4.841982
## 43                1.522000               6.359000
## 44                1.531000               7.169000
## 45                1.533000               6.712000
## 46                1.537000               4.697000
## 47                1.544788               3.019437
## 48                1.550000               2.850000
## 49                1.550993               2.491059
## 50                1.560000               2.690000
## 51                1.564000               2.875000
## 52                1.570000               1.940000
## 53                1.575000               4.786000
## 54                1.580000               3.094000
## 55                1.581000               4.202000
## 56                1.589351               3.285589
## 57                1.590000               2.020000
## 58                1.595000               6.866000
## 59                1.600000               3.253500
## 60                1.610000               1.980000
## 61                1.620000               2.676000
## 62                1.626000               5.345000
## 63                1.633000               4.333000
## 64                1.640000               3.155000
## 65                1.640018               3.668255
## 66                1.641000               6.061000
## 67                1.664000               3.654000
## 68                1.664522               5.727913
## 69                1.669000               5.790000
## 70                1.670000               2.170000
## 71                1.692000               7.052500
## 72                1.693061               2.831429
## 73                1.699000               5.888000
## 74                1.700000               3.453000
## 75                1.717000               6.735000
## 76                1.720000               3.175000
## 77                1.750000               3.498000
## 78                1.778000               6.836000
## 79                1.797000               7.286000
## 80                1.800000               2.340000
## 81                1.801000               6.647000
## 82                1.803000               6.520500
## 83                1.806000               3.328000
## 84                1.809000               3.568000
## 85                1.811000               7.152000
## 86                1.820000               4.290000
## 87                1.822000               6.763000
## 88                1.824446               4.970095
## 89                1.830000               2.470000
## 90                1.835001               3.103683
## 91                1.848000               3.012000
## 92                1.852014               5.959449
## 93                1.853283               5.865889
## 94                1.864022               5.945441
## 95                1.883205               3.184230
## 96                1.885000               3.075000
## 97                1.889000               6.383000
## 98                1.896000               6.885000
## 99                1.944000               6.280000
## 100               1.981000               6.784000
## 101               1.990000               5.465000
## 102               2.004000               6.743000
## 103               2.010000               5.972500
## 104               2.020000               6.278000
## 105               2.026000               6.721000
## 106               2.029000               6.030000
## 107               2.031000               5.921000
## 108               2.081000               2.942000
## 109               2.086000               6.942000
## 110               2.091000               5.818000
## 111               2.110000               7.162000
## 112               2.136727               5.219097
## 113               2.151000               5.983000
## 114               2.175000               5.547000
## 115               2.192000               6.941000
## 116               2.211000               6.358000
## 117               2.240930               6.078350
## 118               2.273000               7.555000
## 119               2.273169               4.695876
## 120               2.321000               7.159000
## 121               2.325000               5.844000
## 122               2.328000               7.040000
## 123               2.344000               6.251000
## 124               2.348000               6.608000
## 125               2.350962               6.166562
## 126               2.363000               7.458000
## 127               2.374000               6.159000
## 128               2.384585               5.251170
## 129               2.393171               5.301390
## 130               2.395000               6.955000
## 131               2.397000               6.372000
## 132               2.415000               5.948000
## 133               2.427000               7.626000
## 134               2.462000               7.373000
## 135               2.469000               6.500000
## 136               2.475000               6.461000
## 137               2.496000               6.292000
## 138               2.569000               5.906000
## 139               2.578281               5.983878
## 140               2.618000               6.358000
## 141               2.623000               7.247000
## 142               2.629064               6.950247
## 143               2.655901               5.357909
## 144               2.660823               7.002555
## 145               2.667000               6.590000
## 146               2.670537               7.002555
## 147               2.711000               6.686000
## 148               2.729000               8.234000
## 149               2.747000               7.485000
## 150               2.748000               7.148000
## 151               2.791000               6.628000
## 152               2.804000               6.828000
## 153               2.814000               6.208000
## 154               2.830000               7.669000
## 155               2.837000               6.827000
## 156               2.839000               6.752000
## 157               2.859614               5.069770
## 158               2.889000               7.503000
## 159               2.890000               5.376000
## 160               2.917000               6.794000
## 161               3.000000               3.866000
## 162               3.018000               5.819000
## 163               3.142565               6.934332
## 164               3.149000               6.319000
## 165               3.163000               4.778000
## 166               3.173000               6.613000
## 167               3.186000               6.547000
## 168               3.215000               6.018000
## 169               3.237000               6.885000
## 170               3.241013               6.768236
## 171               3.303000               6.205000
## 172               3.304000               6.553000
## 173               3.320000               4.530000
## 174               3.335000               7.632000
## 175               3.470000               6.800000
## 176               3.491000               5.821000
## 177               3.496000               5.300000
## 178               3.519000               5.269000
## 179               3.563000               6.847000
## 180               3.735000               6.863000
## 181               3.795000               7.938000
## 182               3.821000               8.187000
## 183               3.823000               6.242000
## 184               3.843961               6.629390
## 185               3.851000               7.300000
## 186               3.867000               6.483000
## 187               3.917000               7.029000
## 188               3.921027               6.622652
## 189               3.930000               7.646000
## 190               3.978000               6.483500
## 191               3.983000               6.970000
## 192               3.983665               6.640794
## 193               4.005000               5.921000
## 194               4.080936               6.607081
## 195               4.089000               6.391000
## 196               4.159000               6.880000
## 197               4.171000               6.085000
## 198               4.257000               6.717000
## 199               4.266000               5.653000
## 200               4.308000               7.115000
## 201               4.354710               6.724125
## 202               4.359060               5.603609
## 203               4.387000               6.996000
## 204               4.398000               6.354000
## 205               4.399000               6.112000
## 206               4.418000               7.691000
## 207               4.457000               6.647000
## 208               4.463000               5.647000
## 209               4.469000               6.721000
## 210               4.585000               6.936000
## 211               4.601289               6.609096
## 212               4.601463               6.609096
## 213               4.620300               6.702820
## 214               4.643000               7.282000
## 215               4.644000               6.315000
## 216               4.644854               6.414706
## 217               4.684000               6.246000
## 218               4.726000               6.725000
## 219               4.772000               6.248000
## 220               4.844171               6.563645
## 221               4.973000               6.282000
## 222               4.978662               6.458448
## 223               5.078000               7.003000
## 224               5.237000               6.364000
## 225               5.304000               6.708000
## 226               5.956000               7.004000
## 227               5.978000               5.814000
## 228               6.156000               6.080000
## 229               6.255000               6.250000
## 230               6.312000               7.250000
## 231               6.820000               7.530000
# Compare median fertility rates in 1960 vs. median fertility in 2021
aggregate(fertility_rates$"1960" ~ fertility_rates$"2021", FUN = median)
##     fertility_rates$"2021" fertility_rates$"1960"
## 1                 0.772000               5.067000
## 2                 0.808000               5.949000
## 3                 0.907000               4.796000
## 4                 1.005000               5.162000
## 5                 1.088000               4.933000
## 6                 1.120000               5.760000
## 7                 1.140000               3.620000
## 8                 1.160000               2.240000
## 9                 1.164000               4.451000
## 10                1.180000               4.820000
## 11                1.190000               2.860000
## 12                1.250000               2.400000
## 13                1.300000               2.755000
## 14                1.321000               3.512000
## 15                1.330000               2.980000
## 16                1.331000               6.248000
## 17                1.340000               2.560000
## 18                1.350000               3.907000
## 19                1.352000               5.580000
## 20                1.380000               2.725000
## 21                1.383908               4.801000
## 22                1.389000               4.818000
## 23                1.390000               4.342500
## 24                1.396865               4.378614
## 25                1.399000               6.967000
## 26                1.410000               6.167000
## 27                1.413000               6.704000
## 28                1.430000               3.811000
## 29                1.442000               4.129000
## 30                1.457000               2.270000
## 31                1.460000               4.719000
## 32                1.480000               2.401500
## 33                1.483000               2.670000
## 34                1.488552               2.897990
## 35                1.489433               4.561606
## 36                1.493000               2.673500
## 37                1.498219               4.531774
## 38                1.504510               2.605864
## 39                1.516846               4.864229
## 40                1.520000               4.159000
## 41                1.520288               2.571186
## 42                1.520442               4.841982
## 43                1.522000               6.359000
## 44                1.531000               7.169000
## 45                1.533000               6.712000
## 46                1.537000               4.697000
## 47                1.544788               3.019437
## 48                1.550000               2.850000
## 49                1.550993               2.491059
## 50                1.560000               2.690000
## 51                1.564000               2.875000
## 52                1.570000               1.940000
## 53                1.575000               4.786000
## 54                1.580000               2.370000
## 55                1.581000               4.202000
## 56                1.589351               3.285589
## 57                1.590000               2.020000
## 58                1.595000               6.866000
## 59                1.600000               3.253500
## 60                1.610000               1.980000
## 61                1.620000               2.676000
## 62                1.626000               5.345000
## 63                1.633000               4.333000
## 64                1.640000               3.040000
## 65                1.640018               3.668255
## 66                1.641000               6.061000
## 67                1.664000               3.654000
## 68                1.664522               5.727913
## 69                1.669000               5.790000
## 70                1.670000               2.170000
## 71                1.692000               7.052500
## 72                1.693061               2.831429
## 73                1.699000               5.888000
## 74                1.700000               3.453000
## 75                1.717000               6.735000
## 76                1.720000               3.175000
## 77                1.750000               3.498000
## 78                1.778000               6.836000
## 79                1.797000               7.286000
## 80                1.800000               2.340000
## 81                1.801000               6.647000
## 82                1.803000               6.520500
## 83                1.806000               3.328000
## 84                1.809000               3.568000
## 85                1.811000               7.152000
## 86                1.820000               4.290000
## 87                1.822000               6.763000
## 88                1.824446               4.970095
## 89                1.830000               2.470000
## 90                1.835001               3.103683
## 91                1.848000               3.012000
## 92                1.852014               5.959449
## 93                1.853283               5.865889
## 94                1.864022               5.945441
## 95                1.883205               3.184230
## 96                1.885000               3.075000
## 97                1.889000               6.383000
## 98                1.896000               6.885000
## 99                1.944000               6.280000
## 100               1.981000               6.784000
## 101               1.990000               5.465000
## 102               2.004000               6.743000
## 103               2.010000               5.972500
## 104               2.020000               6.278000
## 105               2.026000               6.721000
## 106               2.029000               6.030000
## 107               2.031000               5.921000
## 108               2.081000               2.942000
## 109               2.086000               6.942000
## 110               2.091000               5.818000
## 111               2.110000               7.162000
## 112               2.136727               5.219097
## 113               2.151000               5.983000
## 114               2.175000               5.547000
## 115               2.192000               6.941000
## 116               2.211000               6.358000
## 117               2.240930               6.078350
## 118               2.273000               7.555000
## 119               2.273169               4.695876
## 120               2.321000               7.159000
## 121               2.325000               5.844000
## 122               2.328000               7.040000
## 123               2.344000               6.251000
## 124               2.348000               6.608000
## 125               2.350962               6.166562
## 126               2.363000               7.458000
## 127               2.374000               6.159000
## 128               2.384585               5.251170
## 129               2.393171               5.301390
## 130               2.395000               6.955000
## 131               2.397000               6.372000
## 132               2.415000               5.948000
## 133               2.427000               7.626000
## 134               2.462000               7.373000
## 135               2.469000               6.500000
## 136               2.475000               6.461000
## 137               2.496000               6.292000
## 138               2.569000               5.906000
## 139               2.578281               5.983878
## 140               2.618000               6.358000
## 141               2.623000               7.247000
## 142               2.629064               6.950247
## 143               2.655901               5.357909
## 144               2.660823               7.002555
## 145               2.667000               6.590000
## 146               2.670537               7.002555
## 147               2.711000               6.686000
## 148               2.729000               8.234000
## 149               2.747000               7.485000
## 150               2.748000               7.148000
## 151               2.791000               6.628000
## 152               2.804000               6.828000
## 153               2.814000               6.208000
## 154               2.830000               7.669000
## 155               2.837000               6.827000
## 156               2.839000               6.752000
## 157               2.859614               5.069770
## 158               2.889000               7.503000
## 159               2.890000               5.376000
## 160               2.917000               6.794000
## 161               3.000000               3.866000
## 162               3.018000               5.819000
## 163               3.142565               6.934332
## 164               3.149000               6.319000
## 165               3.163000               4.778000
## 166               3.173000               6.613000
## 167               3.186000               6.547000
## 168               3.215000               6.018000
## 169               3.237000               6.885000
## 170               3.241013               6.768236
## 171               3.303000               6.205000
## 172               3.304000               6.553000
## 173               3.320000               4.530000
## 174               3.335000               7.632000
## 175               3.470000               6.800000
## 176               3.491000               5.821000
## 177               3.496000               5.300000
## 178               3.519000               5.269000
## 179               3.563000               6.847000
## 180               3.735000               6.863000
## 181               3.795000               7.938000
## 182               3.821000               8.187000
## 183               3.823000               6.242000
## 184               3.843961               6.629390
## 185               3.851000               7.300000
## 186               3.867000               6.483000
## 187               3.917000               7.029000
## 188               3.921027               6.622652
## 189               3.930000               7.646000
## 190               3.978000               6.483500
## 191               3.983000               6.970000
## 192               3.983665               6.640794
## 193               4.005000               5.921000
## 194               4.080936               6.607081
## 195               4.089000               6.391000
## 196               4.159000               6.880000
## 197               4.171000               6.085000
## 198               4.257000               6.717000
## 199               4.266000               5.653000
## 200               4.308000               7.115000
## 201               4.354710               6.724125
## 202               4.359060               5.603609
## 203               4.387000               6.996000
## 204               4.398000               6.354000
## 205               4.399000               6.112000
## 206               4.418000               7.691000
## 207               4.457000               6.647000
## 208               4.463000               5.647000
## 209               4.469000               6.721000
## 210               4.585000               6.936000
## 211               4.601289               6.609096
## 212               4.601463               6.609096
## 213               4.620300               6.702820
## 214               4.643000               7.282000
## 215               4.644000               6.315000
## 216               4.644854               6.414706
## 217               4.684000               6.246000
## 218               4.726000               6.725000
## 219               4.772000               6.248000
## 220               4.844171               6.563645
## 221               4.973000               6.282000
## 222               4.978662               6.458448
## 223               5.078000               7.003000
## 224               5.237000               6.364000
## 225               5.304000               6.708000
## 226               5.956000               7.004000
## 227               5.978000               5.814000
## 228               6.156000               6.080000
## 229               6.255000               6.250000
## 230               6.312000               7.250000
## 231               6.820000               7.530000
# Compare maximum fertility rates in 1960 vs. maximum fertility rates in 2021
aggregate(fertility_rates$"1960" ~ fertility_rates$"2021", FUN = max)
##     fertility_rates$"2021" fertility_rates$"1960"
## 1                 0.772000               5.067000
## 2                 0.808000               5.949000
## 3                 0.907000               4.796000
## 4                 1.005000               5.162000
## 5                 1.088000               4.933000
## 6                 1.120000               5.760000
## 7                 1.140000               3.620000
## 8                 1.160000               2.240000
## 9                 1.164000               4.451000
## 10                1.180000               4.820000
## 11                1.190000               2.860000
## 12                1.250000               2.400000
## 13                1.300000               3.510000
## 14                1.321000               3.512000
## 15                1.330000               2.980000
## 16                1.331000               6.248000
## 17                1.340000               2.560000
## 18                1.350000               3.907000
## 19                1.352000               5.580000
## 20                1.380000               3.160000
## 21                1.383908               4.801000
## 22                1.389000               4.818000
## 23                1.390000               6.455000
## 24                1.396865               4.378614
## 25                1.399000               6.967000
## 26                1.410000               6.167000
## 27                1.413000               6.704000
## 28                1.430000               3.811000
## 29                1.442000               4.129000
## 30                1.457000               2.270000
## 31                1.460000               6.718000
## 32                1.480000               2.690000
## 33                1.483000               2.670000
## 34                1.488552               2.897990
## 35                1.489433               4.561606
## 36                1.493000               2.827000
## 37                1.498219               4.531774
## 38                1.504510               2.605864
## 39                1.516846               4.864229
## 40                1.520000               5.878000
## 41                1.520288               2.571186
## 42                1.520442               4.841982
## 43                1.522000               6.359000
## 44                1.531000               7.169000
## 45                1.533000               6.712000
## 46                1.537000               4.697000
## 47                1.544788               3.019437
## 48                1.550000               2.850000
## 49                1.550993               2.491059
## 50                1.560000               2.690000
## 51                1.564000               2.875000
## 52                1.570000               1.940000
## 53                1.575000               4.786000
## 54                1.580000               4.602000
## 55                1.581000               4.202000
## 56                1.589351               3.285589
## 57                1.590000               2.020000
## 58                1.595000               6.866000
## 59                1.600000               3.967000
## 60                1.610000               1.980000
## 61                1.620000               3.120000
## 62                1.626000               5.345000
## 63                1.633000               4.333000
## 64                1.640000               4.240000
## 65                1.640018               3.668255
## 66                1.641000               6.061000
## 67                1.664000               3.654000
## 68                1.664522               5.727913
## 69                1.669000               5.790000
## 70                1.670000               2.170000
## 71                1.692000               7.301000
## 72                1.693061               2.831429
## 73                1.699000               5.888000
## 74                1.700000               3.453000
## 75                1.717000               6.735000
## 76                1.720000               3.780000
## 77                1.750000               3.498000
## 78                1.778000               6.836000
## 79                1.797000               7.286000
## 80                1.800000               2.340000
## 81                1.801000               6.647000
## 82                1.803000               6.634000
## 83                1.806000               3.328000
## 84                1.809000               3.568000
## 85                1.811000               7.152000
## 86                1.820000               4.290000
## 87                1.822000               6.763000
## 88                1.824446               4.970095
## 89                1.830000               2.850000
## 90                1.835001               3.103683
## 91                1.848000               3.012000
## 92                1.852014               5.959449
## 93                1.853283               5.865889
## 94                1.864022               5.945441
## 95                1.883205               3.184230
## 96                1.885000               3.075000
## 97                1.889000               6.383000
## 98                1.896000               6.885000
## 99                1.944000               6.280000
## 100               1.981000               6.784000
## 101               1.990000               5.465000
## 102               2.004000               6.743000
## 103               2.010000               6.500000
## 104               2.020000               6.278000
## 105               2.026000               6.721000
## 106               2.029000               6.030000
## 107               2.031000               5.921000
## 108               2.081000               2.942000
## 109               2.086000               6.942000
## 110               2.091000               5.818000
## 111               2.110000               7.162000
## 112               2.136727               5.219097
## 113               2.151000               5.983000
## 114               2.175000               5.547000
## 115               2.192000               6.941000
## 116               2.211000               6.358000
## 117               2.240930               6.078350
## 118               2.273000               7.555000
## 119               2.273169               4.695876
## 120               2.321000               7.159000
## 121               2.325000               5.844000
## 122               2.328000               7.040000
## 123               2.344000               6.251000
## 124               2.348000               6.608000
## 125               2.350962               6.166562
## 126               2.363000               7.458000
## 127               2.374000               6.159000
## 128               2.384585               5.251170
## 129               2.393171               5.301390
## 130               2.395000               6.955000
## 131               2.397000               6.372000
## 132               2.415000               5.948000
## 133               2.427000               7.626000
## 134               2.462000               7.373000
## 135               2.469000               6.500000
## 136               2.475000               6.461000
## 137               2.496000               6.292000
## 138               2.569000               5.906000
## 139               2.578281               5.983878
## 140               2.618000               6.358000
## 141               2.623000               7.247000
## 142               2.629064               6.950247
## 143               2.655901               5.357909
## 144               2.660823               7.002555
## 145               2.667000               6.590000
## 146               2.670537               7.002555
## 147               2.711000               6.686000
## 148               2.729000               8.234000
## 149               2.747000               7.485000
## 150               2.748000               7.148000
## 151               2.791000               6.628000
## 152               2.804000               6.828000
## 153               2.814000               6.208000
## 154               2.830000               7.669000
## 155               2.837000               6.827000
## 156               2.839000               6.752000
## 157               2.859614               5.069770
## 158               2.889000               7.503000
## 159               2.890000               5.376000
## 160               2.917000               6.794000
## 161               3.000000               3.866000
## 162               3.018000               5.819000
## 163               3.142565               6.934332
## 164               3.149000               6.319000
## 165               3.163000               4.778000
## 166               3.173000               6.613000
## 167               3.186000               6.547000
## 168               3.215000               6.018000
## 169               3.237000               6.885000
## 170               3.241013               6.768236
## 171               3.303000               6.205000
## 172               3.304000               6.553000
## 173               3.320000               4.530000
## 174               3.335000               7.632000
## 175               3.470000               6.800000
## 176               3.491000               7.220000
## 177               3.496000               5.300000
## 178               3.519000               5.269000
## 179               3.563000               6.847000
## 180               3.735000               6.863000
## 181               3.795000               7.938000
## 182               3.821000               8.187000
## 183               3.823000               6.242000
## 184               3.843961               6.629390
## 185               3.851000               7.300000
## 186               3.867000               6.483000
## 187               3.917000               7.029000
## 188               3.921027               6.622652
## 189               3.930000               7.646000
## 190               3.978000               6.792000
## 191               3.983000               6.970000
## 192               3.983665               6.640794
## 193               4.005000               5.921000
## 194               4.080936               6.607081
## 195               4.089000               6.391000
## 196               4.159000               6.880000
## 197               4.171000               6.085000
## 198               4.257000               6.717000
## 199               4.266000               5.653000
## 200               4.308000               7.115000
## 201               4.354710               6.724125
## 202               4.359060               5.603609
## 203               4.387000               6.996000
## 204               4.398000               6.354000
## 205               4.399000               6.112000
## 206               4.418000               7.691000
## 207               4.457000               6.647000
## 208               4.463000               5.647000
## 209               4.469000               6.721000
## 210               4.585000               6.936000
## 211               4.601289               6.609096
## 212               4.601463               6.609096
## 213               4.620300               6.702820
## 214               4.643000               7.282000
## 215               4.644000               6.315000
## 216               4.644854               6.414706
## 217               4.684000               6.246000
## 218               4.726000               6.725000
## 219               4.772000               6.248000
## 220               4.844171               6.563645
## 221               4.973000               6.282000
## 222               4.978662               6.458448
## 223               5.078000               7.003000
## 224               5.237000               6.364000
## 225               5.304000               6.708000
## 226               5.956000               7.004000
## 227               5.978000               5.814000
## 228               6.156000               6.080000
## 229               6.255000               6.250000
## 230               6.312000               7.250000
## 231               6.820000               7.530000
# Compare minimum fertility rates in 1960 vs. minimum fertility rates in 2021
aggregate(fertility_rates$"1960" ~ fertility_rates$"2021", FUN = min)
##     fertility_rates$"2021" fertility_rates$"1960"
## 1                 0.772000               5.067000
## 2                 0.808000               5.949000
## 3                 0.907000               4.796000
## 4                 1.005000               5.162000
## 5                 1.088000               4.933000
## 6                 1.120000               5.760000
## 7                 1.140000               3.620000
## 8                 1.160000               2.240000
## 9                 1.164000               4.451000
## 10                1.180000               4.820000
## 11                1.190000               2.860000
## 12                1.250000               2.400000
## 13                1.300000               2.000000
## 14                1.321000               3.512000
## 15                1.330000               2.980000
## 16                1.331000               6.248000
## 17                1.340000               2.560000
## 18                1.350000               3.907000
## 19                1.352000               5.580000
## 20                1.380000               2.290000
## 21                1.383908               4.801000
## 22                1.389000               4.818000
## 23                1.390000               2.230000
## 24                1.396865               4.378614
## 25                1.399000               6.967000
## 26                1.410000               6.167000
## 27                1.413000               6.704000
## 28                1.430000               3.811000
## 29                1.442000               4.129000
## 30                1.457000               2.270000
## 31                1.460000               2.720000
## 32                1.480000               2.113000
## 33                1.483000               2.670000
## 34                1.488552               2.897990
## 35                1.489433               4.561606
## 36                1.493000               2.520000
## 37                1.498219               4.531774
## 38                1.504510               2.605864
## 39                1.516846               4.864229
## 40                1.520000               2.440000
## 41                1.520288               2.571186
## 42                1.520442               4.841982
## 43                1.522000               6.359000
## 44                1.531000               7.169000
## 45                1.533000               6.712000
## 46                1.537000               4.697000
## 47                1.544788               3.019437
## 48                1.550000               2.850000
## 49                1.550993               2.491059
## 50                1.560000               2.690000
## 51                1.564000               2.875000
## 52                1.570000               1.940000
## 53                1.575000               4.786000
## 54                1.580000               2.310000
## 55                1.581000               4.202000
## 56                1.589351               3.285589
## 57                1.590000               2.020000
## 58                1.595000               6.866000
## 59                1.600000               2.540000
## 60                1.610000               1.980000
## 61                1.620000               2.232000
## 62                1.626000               5.345000
## 63                1.633000               4.333000
## 64                1.640000               2.185000
## 65                1.640018               3.668255
## 66                1.641000               6.061000
## 67                1.664000               3.654000
## 68                1.664522               5.727913
## 69                1.669000               5.790000
## 70                1.670000               2.170000
## 71                1.692000               6.804000
## 72                1.693061               2.831429
## 73                1.699000               5.888000
## 74                1.700000               3.453000
## 75                1.717000               6.735000
## 76                1.720000               2.570000
## 77                1.750000               3.498000
## 78                1.778000               6.836000
## 79                1.797000               7.286000
## 80                1.800000               2.340000
## 81                1.801000               6.647000
## 82                1.803000               6.407000
## 83                1.806000               3.328000
## 84                1.809000               3.568000
## 85                1.811000               7.152000
## 86                1.820000               4.290000
## 87                1.822000               6.763000
## 88                1.824446               4.970095
## 89                1.830000               2.090000
## 90                1.835001               3.103683
## 91                1.848000               3.012000
## 92                1.852014               5.959449
## 93                1.853283               5.865889
## 94                1.864022               5.945441
## 95                1.883205               3.184230
## 96                1.885000               3.075000
## 97                1.889000               6.383000
## 98                1.896000               6.885000
## 99                1.944000               6.280000
## 100               1.981000               6.784000
## 101               1.990000               5.465000
## 102               2.004000               6.743000
## 103               2.010000               5.445000
## 104               2.020000               6.278000
## 105               2.026000               6.721000
## 106               2.029000               6.030000
## 107               2.031000               5.921000
## 108               2.081000               2.942000
## 109               2.086000               6.942000
## 110               2.091000               5.818000
## 111               2.110000               7.162000
## 112               2.136727               5.219097
## 113               2.151000               5.983000
## 114               2.175000               5.547000
## 115               2.192000               6.941000
## 116               2.211000               6.358000
## 117               2.240930               6.078350
## 118               2.273000               7.555000
## 119               2.273169               4.695876
## 120               2.321000               7.159000
## 121               2.325000               5.844000
## 122               2.328000               7.040000
## 123               2.344000               6.251000
## 124               2.348000               6.608000
## 125               2.350962               6.166562
## 126               2.363000               7.458000
## 127               2.374000               6.159000
## 128               2.384585               5.251170
## 129               2.393171               5.301390
## 130               2.395000               6.955000
## 131               2.397000               6.372000
## 132               2.415000               5.948000
## 133               2.427000               7.626000
## 134               2.462000               7.373000
## 135               2.469000               6.500000
## 136               2.475000               6.461000
## 137               2.496000               6.292000
## 138               2.569000               5.906000
## 139               2.578281               5.983878
## 140               2.618000               6.358000
## 141               2.623000               7.247000
## 142               2.629064               6.950247
## 143               2.655901               5.357909
## 144               2.660823               7.002555
## 145               2.667000               6.590000
## 146               2.670537               7.002555
## 147               2.711000               6.686000
## 148               2.729000               8.234000
## 149               2.747000               7.485000
## 150               2.748000               7.148000
## 151               2.791000               6.628000
## 152               2.804000               6.828000
## 153               2.814000               6.208000
## 154               2.830000               7.669000
## 155               2.837000               6.827000
## 156               2.839000               6.752000
## 157               2.859614               5.069770
## 158               2.889000               7.503000
## 159               2.890000               5.376000
## 160               2.917000               6.794000
## 161               3.000000               3.866000
## 162               3.018000               5.819000
## 163               3.142565               6.934332
## 164               3.149000               6.319000
## 165               3.163000               4.778000
## 166               3.173000               6.613000
## 167               3.186000               6.547000
## 168               3.215000               6.018000
## 169               3.237000               6.885000
## 170               3.241013               6.768236
## 171               3.303000               6.205000
## 172               3.304000               6.553000
## 173               3.320000               4.530000
## 174               3.335000               7.632000
## 175               3.470000               6.800000
## 176               3.491000               4.422000
## 177               3.496000               5.300000
## 178               3.519000               5.269000
## 179               3.563000               6.847000
## 180               3.735000               6.863000
## 181               3.795000               7.938000
## 182               3.821000               8.187000
## 183               3.823000               6.242000
## 184               3.843961               6.629390
## 185               3.851000               7.300000
## 186               3.867000               6.483000
## 187               3.917000               7.029000
## 188               3.921027               6.622652
## 189               3.930000               7.646000
## 190               3.978000               6.175000
## 191               3.983000               6.970000
## 192               3.983665               6.640794
## 193               4.005000               5.921000
## 194               4.080936               6.607081
## 195               4.089000               6.391000
## 196               4.159000               6.880000
## 197               4.171000               6.085000
## 198               4.257000               6.717000
## 199               4.266000               5.653000
## 200               4.308000               7.115000
## 201               4.354710               6.724125
## 202               4.359060               5.603609
## 203               4.387000               6.996000
## 204               4.398000               6.354000
## 205               4.399000               6.112000
## 206               4.418000               7.691000
## 207               4.457000               6.647000
## 208               4.463000               5.647000
## 209               4.469000               6.721000
## 210               4.585000               6.936000
## 211               4.601289               6.609096
## 212               4.601463               6.609096
## 213               4.620300               6.702820
## 214               4.643000               7.282000
## 215               4.644000               6.315000
## 216               4.644854               6.414706
## 217               4.684000               6.246000
## 218               4.726000               6.725000
## 219               4.772000               6.248000
## 220               4.844171               6.563645
## 221               4.973000               6.282000
## 222               4.978662               6.458448
## 223               5.078000               7.003000
## 224               5.237000               6.364000
## 225               5.304000               6.708000
## 226               5.956000               7.004000
## 227               5.978000               5.814000
## 228               6.156000               6.080000
## 229               6.255000               6.250000
## 230               6.312000               7.250000
## 231               6.820000               7.530000
# Find Average Total Fertility rate by Country Name in 1960 vs. 2021. 
# Assign the result to avg_fertility_rate.
avg_fertility_rate <- aggregate(fertility_rates$"1960" 
                                ~ fertility_rates$"2021" 
                                + fertility_rates$"Country Name", 
                                FUN = mean)

Step 5: Create figures

Average Total Fertility Rate by Country (1960 vs. 2021)

# Create a scatter plot of the data to find general pattern of fertility rates
ggplot(avg_fertility_rate, aes(x = `fertility_rates$"2021"`, y = `fertility_rates$"1960"`)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  xlab("Average Total Fertility Rate in 2021") +
  ylab("Average Total Fertility Rate in 1960") +
  ggtitle("Average Total Fertility Rate by Country (1960 vs. 2021)")
## `geom_smooth()` using formula = 'y ~ x'

Average Total Fertility Rate in European Countries (1960-2021)

# Find unique country names in fertility_rates table
country_names <- unique(fertility_rates$`Country Name`)
sorted_country_names <- sort(country_names)
sorted_country_names
##   [1] "Afghanistan"                                         
##   [2] "Africa Eastern and Southern"                         
##   [3] "Africa Western and Central"                          
##   [4] "Albania"                                             
##   [5] "Algeria"                                             
##   [6] "American Samoa"                                      
##   [7] "Andorra"                                             
##   [8] "Angola"                                              
##   [9] "Antigua and Barbuda"                                 
##  [10] "Arab World"                                          
##  [11] "Argentina"                                           
##  [12] "Armenia"                                             
##  [13] "Aruba"                                               
##  [14] "Australia"                                           
##  [15] "Austria"                                             
##  [16] "Azerbaijan"                                          
##  [17] "Bahamas, The"                                        
##  [18] "Bahrain"                                             
##  [19] "Bangladesh"                                          
##  [20] "Barbados"                                            
##  [21] "Belarus"                                             
##  [22] "Belgium"                                             
##  [23] "Belize"                                              
##  [24] "Benin"                                               
##  [25] "Bermuda"                                             
##  [26] "Bhutan"                                              
##  [27] "Bolivia"                                             
##  [28] "Bosnia and Herzegovina"                              
##  [29] "Botswana"                                            
##  [30] "Brazil"                                              
##  [31] "British Virgin Islands"                              
##  [32] "Brunei Darussalam"                                   
##  [33] "Bulgaria"                                            
##  [34] "Burkina Faso"                                        
##  [35] "Burundi"                                             
##  [36] "Cabo Verde"                                          
##  [37] "Cambodia"                                            
##  [38] "Cameroon"                                            
##  [39] "Canada"                                              
##  [40] "Caribbean small states"                              
##  [41] "Cayman Islands"                                      
##  [42] "Central African Republic"                            
##  [43] "Central Europe and the Baltics"                      
##  [44] "Chad"                                                
##  [45] "Channel Islands"                                     
##  [46] "Chile"                                               
##  [47] "China"                                               
##  [48] "Colombia"                                            
##  [49] "Comoros"                                             
##  [50] "Congo, Dem. Rep."                                    
##  [51] "Congo, Rep."                                         
##  [52] "Costa Rica"                                          
##  [53] "Cote d'Ivoire"                                       
##  [54] "Croatia"                                             
##  [55] "Cuba"                                                
##  [56] "Curacao"                                             
##  [57] "Cyprus"                                              
##  [58] "Czechia"                                             
##  [59] "Denmark"                                             
##  [60] "Djibouti"                                            
##  [61] "Dominica"                                            
##  [62] "Dominican Republic"                                  
##  [63] "Early-demographic dividend"                          
##  [64] "East Asia & Pacific"                                 
##  [65] "East Asia & Pacific (excluding high income)"         
##  [66] "East Asia & Pacific (IDA & IBRD countries)"          
##  [67] "Ecuador"                                             
##  [68] "Egypt, Arab Rep."                                    
##  [69] "El Salvador"                                         
##  [70] "Equatorial Guinea"                                   
##  [71] "Eritrea"                                             
##  [72] "Estonia"                                             
##  [73] "Eswatini"                                            
##  [74] "Ethiopia"                                            
##  [75] "Euro area"                                           
##  [76] "Europe & Central Asia"                               
##  [77] "Europe & Central Asia (excluding high income)"       
##  [78] "Europe & Central Asia (IDA & IBRD countries)"        
##  [79] "European Union"                                      
##  [80] "Faroe Islands"                                       
##  [81] "Fiji"                                                
##  [82] "Finland"                                             
##  [83] "Fragile and conflict affected situations"            
##  [84] "France"                                              
##  [85] "French Polynesia"                                    
##  [86] "Gabon"                                               
##  [87] "Gambia, The"                                         
##  [88] "Georgia"                                             
##  [89] "Germany"                                             
##  [90] "Ghana"                                               
##  [91] "Gibraltar"                                           
##  [92] "Greece"                                              
##  [93] "Greenland"                                           
##  [94] "Grenada"                                             
##  [95] "Guam"                                                
##  [96] "Guatemala"                                           
##  [97] "Guinea"                                              
##  [98] "Guinea-Bissau"                                       
##  [99] "Guyana"                                              
## [100] "Haiti"                                               
## [101] "Heavily indebted poor countries (HIPC)"              
## [102] "High income"                                         
## [103] "Honduras"                                            
## [104] "Hong Kong SAR, China"                                
## [105] "Hungary"                                             
## [106] "IBRD only"                                           
## [107] "Iceland"                                             
## [108] "IDA & IBRD total"                                    
## [109] "IDA blend"                                           
## [110] "IDA only"                                            
## [111] "IDA total"                                           
## [112] "India"                                               
## [113] "Indonesia"                                           
## [114] "Iran, Islamic Rep."                                  
## [115] "Iraq"                                                
## [116] "Ireland"                                             
## [117] "Isle of Man"                                         
## [118] "Israel"                                              
## [119] "Italy"                                               
## [120] "Jamaica"                                             
## [121] "Japan"                                               
## [122] "Jordan"                                              
## [123] "Kazakhstan"                                          
## [124] "Kenya"                                               
## [125] "Kiribati"                                            
## [126] "Korea, Dem. People's Rep."                           
## [127] "Korea, Rep."                                         
## [128] "Kosovo"                                              
## [129] "Kuwait"                                              
## [130] "Kyrgyz Republic"                                     
## [131] "Lao PDR"                                             
## [132] "Late-demographic dividend"                           
## [133] "Latin America & Caribbean"                           
## [134] "Latin America & Caribbean (excluding high income)"   
## [135] "Latin America & the Caribbean (IDA & IBRD countries)"
## [136] "Latvia"                                              
## [137] "Least developed countries: UN classification"        
## [138] "Lebanon"                                             
## [139] "Lesotho"                                             
## [140] "Liberia"                                             
## [141] "Libya"                                               
## [142] "Liechtenstein"                                       
## [143] "Lithuania"                                           
## [144] "Low & middle income"                                 
## [145] "Low income"                                          
## [146] "Lower middle income"                                 
## [147] "Luxembourg"                                          
## [148] "Macao SAR, China"                                    
## [149] "Madagascar"                                          
## [150] "Malawi"                                              
## [151] "Malaysia"                                            
## [152] "Maldives"                                            
## [153] "Mali"                                                
## [154] "Malta"                                               
## [155] "Marshall Islands"                                    
## [156] "Mauritania"                                          
## [157] "Mauritius"                                           
## [158] "Mexico"                                              
## [159] "Micronesia, Fed. Sts."                               
## [160] "Middle East & North Africa"                          
## [161] "Middle East & North Africa (excluding high income)"  
## [162] "Middle East & North Africa (IDA & IBRD countries)"   
## [163] "Middle income"                                       
## [164] "Moldova"                                             
## [165] "Monaco"                                              
## [166] "Mongolia"                                            
## [167] "Montenegro"                                          
## [168] "Morocco"                                             
## [169] "Mozambique"                                          
## [170] "Myanmar"                                             
## [171] "Namibia"                                             
## [172] "Nauru"                                               
## [173] "Nepal"                                               
## [174] "Netherlands"                                         
## [175] "New Caledonia"                                       
## [176] "New Zealand"                                         
## [177] "Nicaragua"                                           
## [178] "Niger"                                               
## [179] "Nigeria"                                             
## [180] "North America"                                       
## [181] "North Macedonia"                                     
## [182] "Northern Mariana Islands"                            
## [183] "Norway"                                              
## [184] "Not classified"                                      
## [185] "OECD members"                                        
## [186] "Oman"                                                
## [187] "Other small states"                                  
## [188] "Pacific island small states"                         
## [189] "Pakistan"                                            
## [190] "Palau"                                               
## [191] "Panama"                                              
## [192] "Papua New Guinea"                                    
## [193] "Paraguay"                                            
## [194] "Peru"                                                
## [195] "Philippines"                                         
## [196] "Poland"                                              
## [197] "Portugal"                                            
## [198] "Post-demographic dividend"                           
## [199] "Pre-demographic dividend"                            
## [200] "Puerto Rico"                                         
## [201] "Qatar"                                               
## [202] "Romania"                                             
## [203] "Russian Federation"                                  
## [204] "Rwanda"                                              
## [205] "Samoa"                                               
## [206] "San Marino"                                          
## [207] "Sao Tome and Principe"                               
## [208] "Saudi Arabia"                                        
## [209] "Senegal"                                             
## [210] "Serbia"                                              
## [211] "Seychelles"                                          
## [212] "Sierra Leone"                                        
## [213] "Singapore"                                           
## [214] "Sint Maarten (Dutch part)"                           
## [215] "Slovak Republic"                                     
## [216] "Slovenia"                                            
## [217] "Small states"                                        
## [218] "Solomon Islands"                                     
## [219] "Somalia"                                             
## [220] "South Africa"                                        
## [221] "South Asia"                                          
## [222] "South Asia (IDA & IBRD)"                             
## [223] "South Sudan"                                         
## [224] "Spain"                                               
## [225] "Sri Lanka"                                           
## [226] "St. Kitts and Nevis"                                 
## [227] "St. Lucia"                                           
## [228] "St. Martin (French part)"                            
## [229] "St. Vincent and the Grenadines"                      
## [230] "Sub-Saharan Africa"                                  
## [231] "Sub-Saharan Africa (excluding high income)"          
## [232] "Sub-Saharan Africa (IDA & IBRD countries)"           
## [233] "Sudan"                                               
## [234] "Suriname"                                            
## [235] "Sweden"                                              
## [236] "Switzerland"                                         
## [237] "Syrian Arab Republic"                                
## [238] "Tajikistan"                                          
## [239] "Tanzania"                                            
## [240] "Thailand"                                            
## [241] "Timor-Leste"                                         
## [242] "Togo"                                                
## [243] "Tonga"                                               
## [244] "Trinidad and Tobago"                                 
## [245] "Tunisia"                                             
## [246] "Turkiye"                                             
## [247] "Turkmenistan"                                        
## [248] "Turks and Caicos Islands"                            
## [249] "Tuvalu"                                              
## [250] "Uganda"                                              
## [251] "Ukraine"                                             
## [252] "United Arab Emirates"                                
## [253] "United Kingdom"                                      
## [254] "United States"                                       
## [255] "Upper middle income"                                 
## [256] "Uruguay"                                             
## [257] "Uzbekistan"                                          
## [258] "Vanuatu"                                             
## [259] "Venezuela, RB"                                       
## [260] "Vietnam"                                             
## [261] "Virgin Islands (U.S.)"                               
## [262] "West Bank and Gaza"                                  
## [263] "World"                                               
## [264] "Yemen, Rep."                                         
## [265] "Zambia"                                              
## [266] "Zimbabwe"
# Filter the dataset to include only European countries
european_countries <- c("Albania", "Andorra", "Austria", "Belarus", "Belgium", 
                        "Bosnia and Herzegovina", "Bulgaria", "Channel Islands", 
                        "Croatia", "Cyprus", "Czechia", "Denmark", "Estonia", 
                        "Faroe Islands", "Finland", "France", "Germany", 
                        "Gibraltar", "Greece", "Hungary", "Iceland", "Ireland",
                        "Isle of Man", "Italy", "Latvia", "Liechtenstein",
                        "Lithuania", "Luxembourg", "Malta", "Moldova",
                        "Monaco", "Montenegro", "Netherlands",
                        "North Macedonia", "Norway", "Poland",
                        "Portugal", "Romania", "Russian Federation",
                        "San Marino", "Serbia","Slovakia","Slovenia","Spain",
                        "Sweden","Switzerland","Ukraine","United Kingdom",
                        "Vatican City")


# Replace with the list of European countries
fertility_rates_europe <- fertility_rates %>%
  filter(`Country Name` %in% european_countries)

# Reshape the data from wide to long format
fertility_rates_long <- fertility_rates_europe %>%
  pivot_longer(cols = `1960`:`2021`,
               names_to = "Year",
               values_to = "Fertility_Rate")

# Create the plot with custom x-axis labels
p <- ggplot(fertility_rates_long, 
            aes(x = Year, 
                y = Fertility_Rate, 
                color = `Country Name`, 
                group = `Country Name`)) +
  geom_line() +
  xlab("Year") +
  ylab("Fertility Rate") +
  ggtitle("Average Total Fertility Rate in European Countries (1960-2021)") +
  scale_x_discrete(breaks = seq(1960, 2021, by = 5)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

p 

# Save the plot to a file with a resolution of 400 dpi
ggsave("Documents/data_analytics/fertility_rate_case_study/fertility_rates_europe.png", 
       plot = p, 
       width = 10, 
       dpi = 1000)
## Saving 10 x 5 in image

Average Total Fertility Rate in African Countries (1960-2021)

# Filter the dataset to include only African countries
african_countries <- c("Angola", "Benin", "Burkina Faso", "Burundi", "Cabo Verde",
                       "Cameroon", "Central African Republic", "Chad", "Comoros",
                       "Congo, Dem. Rep.", "Congo, Rep.", "Cote d'Ivoire",
                       "Djibouti", "Egypt, Arab Rep.", "Equatorial Guinea",
                       "Eritrea", "Eswatini", "Ethiopia", "Gabon", "Gambia, The",
                       "Ghana", "Guinea", "Guinea-Bissau", "Kenya", "Lesotho",
                       "Liberia", "Libya", "Madagascar", "Malawi", "Mali",
                       "Mauritania", "Mauritius", "Morocco", "Mozambique",
                       "Namibia", "Niger", "Nigeria", "Rwanda",
                       "Sao Tome and Principe", "Senegal", 
                       "Seychelles","Sierra Leone","Somalia","South Africa",
                       "South Sudan","Sudan","Tanzania","Togo","Tunisia",
                       "Uganda","Zambia","Zimbabwe")


# Replace with the list of African countries
fertility_rates_africa <- fertility_rates %>%
  filter(`Country Name` %in% african_countries)

# Reshape the data from wide to long format
fertility_rates_long <- fertility_rates_africa %>%
  pivot_longer(cols = `1960`:`2021`,
               names_to = "Year",
               values_to = "Fertility_Rate")

# Create the plot with custom x-axis labels
p <- ggplot(fertility_rates_long, 
            aes(x = Year, 
                y = Fertility_Rate, 
                color = `Country Name`, 
                group = `Country Name`)) +
  geom_line() +
  xlab("Year") +
  ylab("Fertility Rate") +
  ggtitle("Average Total Fertility Rate in African Countries (1960-2021)") +
  scale_x_discrete(breaks = seq(1960, 2021, by = 5)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

p 

# Save the plot to a file with a resolution of 400 dpi
ggsave("Documents/data_analytics/fertility_rate_case_study/fertility_rates_africa.png",
       plot = p,
       width = 10,
       dpi = 1000)
## Saving 10 x 5 in image

Average Total Fertility Rate in Asian Countries (1960-2021)

# Filter the dataset to include only Asian countries
asian_countries <- c("Afghanistan", "Armenia", "Azerbaijan", "Bahrain", "Bangladesh",
                     "Bhutan", "Brunei Darussalam", "Cambodia", "China", "Georgia",
                     "Hong Kong SAR, China", "India", "Indonesia", "Iran, Islamic Rep.",
                     "Iraq", "Israel", "Japan", "Jordan", "Kazakhstan",
                     "Korea, Dem. People's Rep.", "Korea, Rep.", "Kuwait",
                     "Kyrgyz Republic", "Lao PDR", "Lebanon",
                     "Macao SAR, China", "Malaysia", "Maldives",
                     "Mongolia", "Myanmar", "Nepal",
                     "Oman", "Pakistan","Palestine","Philippines","Qatar",
                     "Russian Federation","Saudi Arabia","Singapore","Sri Lanka",
                     "Syrian Arab Republic","Taiwan, China","Tajikistan","Thailand",
                     "Timor-Leste","Turkey","Turkmenistan","United Arab Emirates",
                     "Uzbekistan","Vietnam","Yemen, Rep.")


# Replace with the list of Asian countries
fertility_rates_asia <- fertility_rates %>%
  filter(`Country Name` %in% asian_countries)

# Reshape the data from wide to long format
fertility_rates_long <- fertility_rates_asia %>%
  pivot_longer(cols = `1960`:`2021`,
               names_to = "Year",
               values_to = "Fertility_Rate")

# Create the plot with custom x-axis labels
p <- ggplot(fertility_rates_long, 
            aes(x = Year, 
                y = Fertility_Rate, 
                color = `Country Name`, 
                group = `Country Name`)) +
  geom_line() +
  xlab("Year") +
  ylab("Fertility Rate") +
  ggtitle("Average Total Fertility Rate in Asian Countries (1960-2021)") +
  scale_x_discrete(breaks = seq(1960, 2021, by = 5)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

p 

# Save the plot to a file with a resolution of 400 dpi
ggsave("Documents/data_analytics/fertility_rate_case_study/fertility_rates_asia.png",
       plot = p,
       width = 10,
       dpi = 1000)
## Saving 10 x 5 in image

Average Total Fertility Rate in North American Countries (1960-2021)

# Filter the dataset to include only North American countries
north_american_countries <- c("Antigua and Barbuda", "Bahamas, The", "Barbados",
                              "Belize", "Canada", "Costa Rica", "Cuba", "Dominica",
                              "Dominican Republic", "El Salvador", "Greenland",
                              "Grenada", "Guatemala", "Haiti", "Honduras",
                              "Jamaica", "Mexico", "Nicaragua", "Panama",
                              "St. Kitts and Nevis", "St. Lucia",
                              "St. Vincent and the Grenadines",
                              "Trinidad and Tobago", "United States")


# Replace with the list of North American countries
fertility_rates_north_america <- fertility_rates %>%
  filter(`Country Name` %in% north_american_countries)

# Reshape the data from wide to long format
fertility_rates_long <- fertility_rates_north_america %>%
  pivot_longer(cols = `1960`:`2021`,
               names_to = "Year",
               values_to = "Fertility_Rate")

# Create the plot with custom x-axis labels
p <- ggplot(fertility_rates_long, 
            aes(x = Year, 
                y = Fertility_Rate, 
                color = `Country Name`, 
                group = `Country Name`)) +
  geom_line() +
  xlab("Year") +
  ylab("Fertility Rate") +
  ggtitle("Average Total Fertility Rate in North American Countries (1960-2021)") +
  scale_x_discrete(breaks = seq(1960, 2021, by = 5)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

p 

# Save the plot to a file with a resolution of 400 dpi
ggsave("Documents/data_analytics/fertility_rate_case_study/fertility_rates_north_america.png",
       plot = p,
       width = 10,
       dpi = 1000)
## Saving 10 x 5 in image

Average Total Fertility Rate in South American Countries (1960-2021)

# Filter the dataset to include only South American countries
south_american_countries <- c("Argentina", "Bolivia", "Brazil", "Chile",
                              "Colombia", "Ecuador", "Guyana", "Paraguay",
                              "Peru", "Suriname", "Uruguay", "Venezuela")


# Replace with the list of South American countries
fertility_rates_south_america <- fertility_rates %>%
  filter(`Country Name` %in% south_american_countries)

# Reshape the data from wide to long format
fertility_rates_long <- fertility_rates_south_america %>%
  pivot_longer(cols = `1960`:`2021`,
               names_to = "Year",
               values_to = "Fertility_Rate")

# Create the plot with custom x-axis labels
p <- ggplot(fertility_rates_long, 
            aes(x = Year, 
                y = Fertility_Rate, 
                color = `Country Name`, 
                group = `Country Name`)) +
  geom_line() +
  xlab("Year") +
  ylab("Fertility Rate") +
  ggtitle("Average Total Fertility Rate in South American Countries (1960-2021)") +
  scale_x_discrete(breaks = seq(1960, 2021, by = 5)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

p 

# Save the plot to a file with a resolution of 400 dpi
ggsave("Documents/data_analytics/fertility_rate_case_study/fertility_rates_south_america.png",
       plot = p,
       width = 10,
       dpi = 1000)
## Saving 10 x 5 in image

Average Total Fertility Rate in Oceanic Countries (1960-2021)

# Filter the dataset to include only Oceanic countries
oceanic_countries <- c("Australia", "Fiji", "Kiribati", "Marshall Islands",
                       "Micronesia, Fed. Sts.", "Nauru", "New Zealand", "Palau",
                       "Papua New Guinea", "Samoa", "Solomon Islands", "Tonga",
                       "Tuvalu", "Vanuatu")


# Replace with the list of Oceanic countries
fertility_rates_oceania <- fertility_rates %>%
  filter(`Country Name` %in% oceanic_countries)

# Reshape the data from wide to long format
fertility_rates_long <- fertility_rates_oceania %>%
  pivot_longer(cols = `1960`:`2021`,
               names_to = "Year",
               values_to = "Fertility_Rate")

# Create the plot with custom x-axis labels
p <- ggplot(fertility_rates_long, 
            aes(x = Year, 
                y = Fertility_Rate, 
                color = `Country Name`, 
                group = `Country Name`)) +
  geom_line() +
  xlab("Year") +
  ylab("Fertility Rate") +
  ggtitle("Average Total Fertility Rate in Oceanic Countries (1960-2021)") +
  scale_x_discrete(breaks = seq(1960, 2021, by = 5)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

p 

# Save the plot to a file with a resolution of 400 dpi
ggsave("Documents/data_analytics/fertility_rate_case_study/fertility_rates_oceania.png",
       plot = p,
       width = 10,
       dpi = 1000)
## Saving 10 x 5 in image

Average Total Fertility Rate by Geographic Region (1960-2021)

# Combine the data for all regions into one data frame
fertility_rates_all_regions <- bind_rows(
  fertility_rates_africa %>% mutate(Region = "Africa"),
  fertility_rates_asia %>% mutate(Region = "Asia"),
  fertility_rates_europe %>% mutate(Region = "Europe"),
  fertility_rates_north_america %>% mutate(Region = "North America"),
  fertility_rates_oceania %>% mutate(Region = "Oceania"),
  fertility_rates_south_america %>% mutate(Region = "South America")
)

# Reshape the data from wide to long format
fertility_rates_long <- fertility_rates_all_regions %>%
  pivot_longer(cols = `1960`:`2021`,
               names_to = "Year",
               values_to = "Fertility_Rate")

# Create the plot with custom x-axis labels and facets
p1 <- ggplot(fertility_rates_long, 
             aes(x = Year, 
                 y = Fertility_Rate, 
                 color = `Country Name`, 
                 group = `Country Name`)) +
  geom_line() +
  xlab("Year") +
  ylab("Fertility Rate") +
  ggtitle("Average Total Fertility Rate by Region (1960-2021)") +
  scale_x_discrete(breaks = seq(1960, 2021, by = 5)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "none") +
  facet_wrap(~ Region)

p1

# Save the plot with the graphs to a file with a resolution of 400 dpi
ggsave("Documents/data_analytics/fertility_rate_case_study/fertility_rates_all_regions_graphs.png",
       plot = p1,
       width = 10,
       dpi = 1000)
## Saving 10 x 5 in image
# Create a separate plot for the legend

Step 6: Export data, create Tableau dashboard

# Overall, it seems that many countries are experiencing a pronounced downward 
# trend in terms of average total fertility rate. To make this more clear, 
# let's export fertility_rates_long.csv to Tableau to create an interactive 
# dashboard. 

write.csv(fertility_rates_long, 
          "Documents/data_analytics/fertility_rate_case_study/fertility_rates_all_regions.csv",
          row.names = FALSE)

Step 7: Explore Tableau dashboard